# encoding=utf8
import sys,threading,easygui,csv,multiprocessing,datetime
import time

from Automation_frame.Qt_designer.Ui_Automation import Ui_Automation
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from Automation_frame.Runner.Actuator import runner
from Automation_frame.collection.data_collection import data_Collector
from PySide2.QtCore import QFile
class UI_app(QWidget,Ui_Automation):
    def __init__(self):
        super(UI_app, self).__init__()
        self.runner=runner()
        self.Collector=data_Collector()
        # self.setupUi(self)
    def start(self):
        '''
        启动函数
        :return:
        '''
        #获取文本框的字符串
        text_path,sheetname,text_mark=self.text_path.text(),self.text_sheetname.text(),self.text_mark.text()
        #执行运行
        # threading.Thread(target=self.runner.begin,args=(text_path,sheetname,text_mark)).start()
        # start=multiprocessing.Process(target=self.runner.begin,args=(text_path,sheetname,text_mark))
        # start.start()
        now_time1 = time.time()    #开始执行的时间时间戳

        text=self.runner.begin(text_path,sheetname,text_mark)    #执行begin
        end_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        # now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')   #开始执行的时间
        self.text_result.clear()    #清空输出框内容
        self.text_result.append(f'-----------------开始执行GUI测试')
        for i in text:
            self.text_result.append(str(i))
        end_time1=time.time()
        self.text_result.append(f'--------已结束完成所有用例，结束时间：{end_time}')
        self.text_result.append(f'----------本次执行共使用时间：{end_time1-now_time1}')
        # start=multiprocessing.pool.ApplyResult
    def get_text(self):
        while True:
            text=self.Collector.print_rlt()
            return text
    def read_file(self):
        with open('../test_data/result_data.txt','r') as file:
            data=[i for i in csv.reader(file)]
            return data
    def browse(self):
        sFilePath = easygui.fileopenbox()
        self.get_texe_path(sFilePath)
        # threading.Thread(target=self.get_texe_path(sFilePath)).start()

    def get_texe_path(self,fname):
        self.text_path.setText(fname)    #给path文本框赋值
    def binding(self):
        '''
        按钮与方法绑定
        :return:
        '''
        #执行按钮绑定函数
        self.button_start.clicked.connect(ui.start)
        # self.button_start.clicked.connect(ui.get_text)
        # 浏览按钮绑定函数
        self.button_browse.clicked.connect(ui.browse)
        #SheetName输入框设置默认参数
        self.text_sheetname.setText('KDT')
        # sheetname=self.text_sheetname.toPlainText()

    def set_text(self,text):
        self.text_result.append(str(text))
if __name__ == '__main__':

    app=QApplication(sys.argv)
    mainwindows=QMainWindow()
    ui=UI_app()
    ui.setupUi(mainwindows)
    ui.binding()
    mainwindows.show()
    sys.exit(app.exec_())
